package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.mapper.ClassifyMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 分类
 * @author  guochao
 * @date  2022/2/2 18:47
 * @version 1.0
 */
@Service
public class ClassifyService {
    
    @Resource
    private ClassifyMapper classifyMapper;

    /**
     * 列表查询
     */
    public void listPage(ClassifyDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<ClassifyDto> classifyList = classifyMapper.list(query);
        PageInfo<ClassifyDto> pageInfo = new PageInfo<>(classifyList);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(classifyList);
    }

    //列表
    public List<ClassifyDto> list(ClassifyDto query){
        return classifyMapper.list(query);
    }

    //列表
    public List<ClassifyDto> listTree(ClassifyDto query){
        return classifyMapper.listTree(query);
    }

    /**
     * id查询
     */
    public ClassifyDto getById(String id) {
        ClassifyDto classifyDto = classifyMapper.getById(id);
        return classifyDto;
    }

    //保存
    public void save(ClassifyDto classifyDto) {
        if (ObjectUtils.isEmpty(classifyDto.getParentId())){
            classifyDto.setParentId("00000000");
        }
        if (StringUtils.isNotBlank(classifyDto.getId())) {
            classifyDto.setUpdateTime(new Date());
            classifyDto.setLastUpdateUserId(classifyDto.getCurrentUserId());
            classifyMapper.update(classifyDto);
        } else {
            classifyDto.setId(UuidUtil.getShortUuid());
            classifyDto.setCreateTime(new Date());
            classifyDto.setCreateUserId(classifyDto.getCurrentUserId());
            classifyMapper.insert(classifyDto);
        }
    }

    //删除
    public void delete(String[] ids) {
        classifyMapper.delete(ids);
    }

    public List<ClassifyDto> getChildListByParentId(String parentId) {
        return classifyMapper.getChildListByParentId(parentId);
    }
}

